import os
import re
import Logger

"""
This function handles a command from a client.
Parses it and performs the desired action.
Either a;
 * Pointer Movement command
 * Keyboard Input command
 * Lirc Command
 * Custom Command
"""
def clientCommand(cmd,optdb):
    
    cmdsplit = cmd.split()
    
    if cmdsplit[0] == 'POINTER':
        pointerCommand(cmd, optdb)
        return
    
    elif cmdsplit[0] == 'KEYBOARD':
        keyboardCommand(cmd, optdb)
        return

    try:
        lirc_cmd = optdb['lirc_cmds'][cmd]
        irremoteCommand(cmd, optdb)
        return
    except KeyError:
        #check if custom command
        try:
            custom_cmd = optdb['custom_cmds'][cmd]
            customCommand(cmd, optdb)
            return
        except KeyError:
            Logger.printLog("Command not recognised: "+cmd)


"""
Handles commands for the pointer device
Expects cmd is of form 
* POINTER MOVE X Y
* POINTER CLICK L|M|R
* POINTER DOWN L|M|R
* POINTER UP L|M|R
"""
def pointerCommand(cmd,optdb):
    
    cmdsplit = cmd.split()
    
    if len(cmdsplit) < 3 or  cmdsplit[0] != 'POINTER':
        Logger.printLog("Badly formed pointer cmd: "+cmd)
        return
    
    prefix = ""
    try:
        prefix = "DISPLAY="+optdb['xsession']+" xte "
    except KeyError:
        prefix = "xte "
    
    if len(cmdsplit) == 3 and (cmdsplit[1] == 'CLICK' or cmdsplit[1] == 'DOWN' or cmdsplit[1] == 'UP'):
        if cmdsplit[2] == 'L':
            btn = 1
        elif cmdsplit[2] == 'M':
            btn = 2
        elif cmdsplit[2] == 'R':
            btn = 3
        if cmdsplit[1] == 'CLICK':
            sys_cmd = prefix + "'mouseclick " + repr(btn) + "'"
        elif cmdsplit[1] == 'DOWN':
            sys_cmd = prefix + "'mousedown " + repr(btn) + "'"
        elif cmdsplit[1] == 'UP':
            sys_cmd = prefix + "'mouseup " + repr(btn) + "'"
        os.system(sys_cmd)
        Logger.printLog("Ran: " + sys_cmd)

    elif len(cmdsplit) == 4 and cmdsplit[1] == 'MOVE':
        sys_cmd =  prefix + "'mousermove "+cmdsplit[2]+" "+cmdsplit[3]+"'"
        os.system(sys_cmd)
        Logger.printLog("Ran: " + sys_cmd)
        
    else:
        Logger.printLog("Badly formed pointer cmd: "+cmd)

"""
Handles commands for the keyboard device
Expects cmd is of form
* KEYBOARD STRING 
"""
def keyboardCommand(cmd,optdb):
    
    cmdsplit = cmd.split()
    
    if len(cmdsplit) < 2 or  cmdsplit[0] != 'KEYBOARD':
        Logger.printLog("Badly formed keyboard cmd: "+cmd)
        return
    
    prefix = ""
    try:
        prefix = "DISPLAY="+optdb['xsession']+" xte "
    except KeyError:
        prefix = "xte "
    
    cmd_string = cmd[len(cmdsplit[0])+1:]
    sys_cmd =  prefix + "'str " + cmd_string + "'"
    os.system(sys_cmd)
    Logger.printLog("Ran: " + sys_cmd)
    
    return

"""
Handles commands for the IR remote interface (LIRC)
"""
def irremoteCommand(cmd, optdb):
    lirc_cmd = optdb['lirc_cmds'][cmd]
    #irsend simulate "000000037ff07be1 00 Up mceusb"
    sys_cmd = "irsend simulate \"" + lirc_cmd[0] + " 00 " + lirc_cmd[1] + " " + optdb['remote'] + "\""
    os.system(sys_cmd)
    Logger.printLog("Ran: " + sys_cmd)

"""
Handles Custom Commmands from the config file
"""
def customCommand(cmd, optdb):
    try:
        custom_cmd = optdb['custom_cmds'][cmd]
        os.system(custom_cmd)
        Logger.printLog("Ran: "+ custom_cmd)
    except KeyError:
        Logger.printLog("Command not recognised: "+cmd)
    except:
        Logger.printLog("Error Running custom cmd:"+custom_cmd)
    
    

